package com.imooc.base;

import com.github.pagehelper.PageInfo;
import com.imooc.utils.PagedGridResult;
import com.imooc.utils.RedisOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class BaseInfoProperties {
    @Autowired
    public RedisOperator redis;
    public static final Integer COMMON_START_PAGE = 1;
    public static final Integer COMMON_START_MONGO_PAGE = 0;
    public static final Integer COMMON_PAGE_SIZE = 10;
    public static final String MOBILE_SMSCODE = "mobile:smscode";
    public static final String REDIS_USER_TOKEN = "redis_user_token";
    public static final String REDIS_USER_INFO = "redis_user_info";
    // 我的关注总数
    public static final String REDIS_MY_FOLLOWS_COUNTS = "redis_my_follows_counts";
    // 我的粉丝总数
    public static final String REDIS_MY_FANS_COUNTS = "redis_my_fans_counts";
    // 博主和粉关联关系 用户判断他们是否互粉
    public static final String REDIS_USER_AND_VLOGGER_RELATIONSHIP = "redis_user_and_vlogger_relationship";
    // 视频和发布者获赞数
    public static final String REDIS_VLOG_BE_LIKED_COUNTS = "redis_vlog_be_liked_counts";
    public static final String REDIS_VLOGGER_BE_LIKED_COUNTS = "redis_vlogger_be_liked_counts";
    public static final String REDIS_USER_LIKE_VLOG = "redis_user_like_vlog";
    // 用户评论总数
    public static final String REDIS_USER_COMMENT_COUNTS = "redis_user_comment_counts";
    // 用户评论被点赞总数
    public static final String REDIS_USER_COMMENT_BE_LIKED_COUNTS = "redis_user_comment_be_liked_counts";
    // 用户是否点赞评论
    public static final String REDIS_USER_BE_LIKE_THIS_COMMENT = "redis_user_be_like_this_comment";
    public Map<String,String> getErrors(BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        Map<String,String> map = new HashMap<>();
        for (FieldError fieldError : fieldErrors){
            log.info(fieldError.getField()+fieldError.getDefaultMessage());
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }
        return map;
    }
    public PagedGridResult setterPagedGrid(List<?>list,Integer page){
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult gridResult = new PagedGridResult();
        gridResult.setRows(list);
        gridResult.setPage(page);
        gridResult.setRecords(pageList.getTotal());
        gridResult.setTotal(pageList.getPages());
        return gridResult;
    }
}
